package com.krt.webapp.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.krt.webapp.R;
import com.krt.webapp.until.AppInfo;
import com.krt.webapp.until.MyApplication;
import com.lxlib.myalbumlib.AlbumConstant;
import com.lxlib.myalbumlib.AlbumConstant.Album;
import com.lxlib.myalbumlib.PicsFragment_MA;
import com.lxlib.myalbumlib.PicsFragment_MA.pfListener;

public class PicSelectActivity extends AppCompatActivity implements pfListener, OnClickListener {

    private PicsFragment_MA pf;
    private Button update_bt, return_bt;
    private int PICNO;
    private MyApplication myApplication;

    private ArrayList<String> dataList = new ArrayList<String>();
    private ArrayList<String> urlList = new ArrayList<String>();
    private ArrayList<Map<String, String>> updateList = new ArrayList<Map<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_select);
        myApplication = new MyApplication(getApplication());
        PICNO = myApplication.getPicno();
        AppInfo appInfo = myApplication.getAppInfo();

        //初始化相册模块属性
        AlbumConstant.DEFAULT_PHOTO = R.mipmap.camera_default;
        AlbumConstant.MAIN_COLOR = R.color.colorPrimary;
        AlbumConstant.PATH = appInfo.getPath();
        AlbumConstant.UPDATA_URL = myApplication.getPIC_URL();

        FragmentManager fm = getSupportFragmentManager();
        if (savedInstanceState == null) {
            pf = new PicsFragment_MA();
            fm.beginTransaction().add(R.id.f1, pf, "pf").commit();
        } else {
            pf = (PicsFragment_MA) fm.findFragmentByTag("pf");
        }
        pf.setPicNum(PICNO);
        pf.setFileKey("img");
        pf.setCamera_mode(PicsFragment_MA.CAMERA_IN);
        pf.setpListener(this);

        updateList = (ArrayList<Map<String, String>>) getIntent().getSerializableExtra("dataList");
        if (updateList.size() != 0) {
            for (int i = 0; i < updateList.size(); i++) {
                dataList.add(updateList.get(i).get("picpath"));
                urlList.add(updateList.get(i).get("upname"));
            }
        }

        pf.setPicpaths(dataList);
        pf.setUrls(urlList);
//		pf.refreshPics2();
        Log.w("tt", "onCreate");
        update_bt = (Button) findViewById(R.id.update_button);
        update_bt.setOnClickListener(this);
        return_bt = (Button) findViewById(R.id.mback_bt);
        return_bt.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        // TODO 自动生成的方法存根
        super.onActivityResult(arg0, arg1, arg2);
        pf.onActivityResult(arg0, arg1, arg2);
    }

    @Override
    public void updateComplet(int state) {
        if (state == PicsFragment_MA.SUCCESS) {
            dataList = pf.getPicpaths();
            urlList = pf.getUrls();
            updateList = new ArrayList<Map<String, String>>();
            Map<String, String> map;
            for (int i = 0; i < dataList.size(); i++) {
                map = new HashMap<String, String>();
                map.put("picpath", dataList.get(i));
                map.put("upname", urlList.get(i));
                updateList.add(map);
            }

            Intent intent = new Intent();
            intent.putExtra("dataList", updateList);
            setResult(RESULT_OK, intent);
            finish();
        } else {
            Toast.makeText(this, "上传失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void goAlbum(Intent i) {
        i.setClass(this, AlbumsActivity.class);
        startActivityForResult(i, Album.XC);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.update_button) {
            pf.updatePics();
        } else if (v.getId() == R.id.mback_bt) {
            setResult(RESULT_CANCELED);
            finish();
        }
    }
}
